package com.oim.fx.ui.chat;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.oim.core.common.data.FaceData;
import com.oim.fx.common.box.ImageBox;
import com.oim.fx.ui.component.ImageStyleButton;
import com.oim.fx.ui.component.event.EventAction;
import com.only.fx.OnlyPopupOver;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.stage.Window;

/**
 * @author: XiaHui
 * @date: 2017年8月21日 下午4:30:21
 */
public class FacePopup {

	FacePanel fp = new FacePanel();
	OnlyPopupOver po = new OnlyPopupOver();

	Set<EventAction<FaceData>> set = new HashSet<EventAction<FaceData>>();
	EventHandler<ActionEvent> action = new EventHandler<ActionEvent>() {

		@Override
		public void handle(ActionEvent event) {
			// TODO 自动生成的方法存根

		}
	};
	EventAction<FaceData> eventAction;

	public FacePopup() {
		po.setArrowLocation(OnlyPopupOver.ArrowLocation.BOTTOM_CENTER);
		po.setContentNode(fp);
		po.setArrowSize(0);
	}

	public final void show(Window ownerWindow, double anchorX, double anchorY) {
		po.show(ownerWindow, anchorX, anchorY);
	}

	public final void show(Node owner, double x, double y) {
		po.show(owner, x, y);
	}

	public final void show(Node owner) {
		po.show(owner);
	}

	public void set(String key, String name, List<FaceData> list) {
		List<ImageStyleButton> faceList = new ArrayList<ImageStyleButton>();

		if (null != list && !list.isEmpty()) {
			for (FaceData data : list) {
				// String faceKey = data.getKey();
				String normalPath = data.getNormalPath();
				String hoverPath = data.getHoverPath();
				String text = data.getText();
				double width = data.getWidth();
				double height = data.getHeight();

				double imageWidth = data.getImageWidth();
				double imageHeight = data.getImageHeight();

				Image normalImage = ImageBox.getImagePath(normalPath);
				Image hoverImage = ImageBox.getImagePath(hoverPath);
				Tooltip tooltip = new Tooltip(text);

				ImageStyleButton button = new ImageStyleButton(normalImage, hoverImage, null);
				button.setTooltip(tooltip);

				if (width > 0 && height > 0) {
					button.setPrefSize(width, height);
					button.setMinSize(width, height);
				}

				if (imageWidth > 0 && imageHeight > 0) {
					button.setImageSize(imageWidth, imageHeight);
				}
				//

				button.setOnAction(a -> {
					po.hide();
					handle(key, data);
				});
				faceList.add(button);
			}
		}
		fp.set(key, name, faceList);
	}

	public void setAction(EventAction<FaceData> a) {
		this.eventAction = a;
		// set.add(a);
	}

	private void handle(String key, FaceData FaceData) {
		if (null != eventAction) {
			eventAction.execute(FaceData);
		}
		// for (EventAction<FaceData> ea : set) {
		// ea.execute(FaceData);
		// }
	}
}
